use fory::ForyObject;
use fory::TypeId;
use fory::{Error, Fory, Reader};

#[derive(ForyObject, Debug, PartialEq)]
struct User {
    name: String,
    age: i32,
    email: String,
}

fn main() -> Result<(), Error> {
    let mut fory = Fory::default();
    fory.register::<User>(1)?;

    let user1 = User {
        name: "Alice1".to_string(),
        age: 31,
        email: "alice1@example.com".to_string(),
    };

    let user2 = User {
        name: "Alice2".to_string(),
        age: 32,
        email: "alice2@example.com".to_string(),
    };

    let user3 = User {
        name: "Alice3".to_string(),
        age: 33,
        email: "alice3@example.com".to_string(),
    };

    let bytes = fory.serialize(&user1)?;

    let decoded: User = fory.deserialize(&bytes)?;
    assert_eq!(user1, decoded);

    let mut buf: Vec<u8> = vec![];
    fory.serialize_to(&user1, &mut buf)?;
    fory.serialize_to(&user2, &mut buf)?;
    fory.serialize_to(&user3, &mut buf)?;
    fory.serialize_to(&"hello".to_string(), &mut buf)?;

    let mut reader = Reader::new(&buf);

    let decoded1: User = fory.deserialize_from(&mut reader)?;
    assert_eq!(user1, decoded1);
     println!("{:?}", decoded1);
    let decoded2: User = fory.deserialize_from(&mut reader)?;
    println!("{:?}", decoded2);
    let decoded3: User = fory.deserialize_from(&mut reader)?;
    println!("{:?}", decoded3);
    let decoded4: String = fory.deserialize_from(&mut reader)?;
    println!("{:?}", decoded4);

    Ok(())
}
